import type { Effect, Reducer } from 'umi';
import { requestCode } from '@/utils/constans';
import { getStaffInfo, getStaffInfoSource } from '@/services/user';
import { getCheckLoginUrl } from '@/services/login';

export type StaffInfoState = {
  id?: string;
  staffName?: string;
  staffCode?: string;
  staffType?: string;
  loginName?: string;
  orgId?: string;
  orgCode?: string;
  sex?: string;
  serialNumber?: string;
  userIdc?: string;
  email?: string;
  birthday?: string | null;
  managerStaffId?: string | null;
  hireDate?: string | null;
  staffResCode?: string[] | [];
  startDate?: string;
  endDate?: string;
  status?: string | null;
  createStaffId?: string | null;
  createTime?: string;
  updateStaffId?: string | null;
  updateTime?: string | null;
  staffPortrait?: string | null;
};

export type StaffInfoSourceState = {
  id?: string;
  tenantId?: string;
  tenantCode?: string;
  resourceName?: string;
  resourceCode?: string;
  resourceType?: string;
  resourceClass?: string;
  pid?: string | null;
  appId?: string | null;
  appName?: string;
  appCode?: string | null;
  menuRul?: string | null;
  compPath?: string | null;
  openType?: string;
  startDate?: string;
  endDate?: string;
  classCode?: string;
  resSort?: string | null;
  status?: string;
  createStaffId?: string;
  createTime?: string;
  updateStaffId?: string;
  updateTime?: string;
  orgCode?: string;
};

export type UserModelState = {
  staffInfo?: StaffInfoState;
  staffInfoSource?: StaffInfoSourceState;
};

export type UserModelType = {
  namespace: 'user';
  state: UserModelState;
  effects: {
    staffInfoFetch: Effect;
    staffInfoSourceFetch: Effect;
    checkLoginUrl: Effect;
  };
  reducers: {
    saveStaffInfo: Reducer<UserModelState>;
    saveStaffInfoResource: Reducer<UserModelState>;
  };
};

const UserModel: UserModelType = {
  namespace: 'user',
  state: {
    staffInfo: {},
    staffInfoSource: {},
  },

  effects: {
    *staffInfoFetch(_, { call, put }) {
      const response = yield call(getStaffInfo);
      const { data, status } = response;
      sessionStorage.setItem('staffInfo', JSON.stringify(data));
      if (status === '00000') {
        // message.success(`🎉 🎉 🎉  ${requestCode[status]}！`);
        yield put({
          type: 'saveStaffInfo',
          payload: data,
        });
        return Promise.resolve(response);
      }
      if (status === 'A0509') {
        return Promise.resolve({ data, status });
      }
      return Promise.reject(requestCode[status]);
    },
    *staffInfoSourceFetch(_, { call, put }) {
      const response = yield call(getStaffInfoSource);
      const { data, status } = response;
      if (status === '00000') {
        // message.success(`🎉 🎉 🎉  ${requestCode[status]}！`);
        yield put({
          type: 'saveStaffInfoResource',
          payload: data,
        });
        return Promise.resolve(data);
      }
      return Promise.reject(requestCode[status]);
    },
    *checkLoginUrl(_, { call, put }) {
      const response = yield call(getCheckLoginUrl);
      const { data, status } = response;

      if (status === '00000') {
        return Promise.resolve(data);
      }
      return Promise.reject(status ? requestCode[status] : '出错啦');
    },
  },

  reducers: {
    saveStaffInfo(state, { payload }) {
      return {
        ...state,
        staffInfo: payload || {},
        // staffInfo: payload.staffInfo || {},
      };
    },
    saveStaffInfoResource(state, { payload }) {
      return {
        ...state,
        staffInfoSource: payload || {},
      };
    },
  },
};

export default UserModel;
